1340B - Nastya and Scoreboard - CodeForces Solution


bitmasks dp graphs greedy *1700

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define momen ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl "\n"
#define fp(n) for(int i=0;i<n;i++)
#define fp1(n) for(int i=1;i<=n;i++)
#define all(v) v.begin() , v.end()
#define int long long
//ll n,m,k,q,y;
ll  mod = 998244353,inf = 1e16+6 ;
const  int N = 5e6+5  ;
vector<int>primes;
int prime[N];
int pre[N];




void seive(){
  //  cout<<"hello\n";
    for(int i=1;i<N;i++){
        prime[i] = i ;
    }
    for(int i=2;i<N ;i++){
        if (prime[i] != i)
            continue;
        for( int j = i*i ;j<N;j+=i){
            prime[j]=i ;
        }
    }
}



void files(){
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
}


int dx[] = {1,-1,0,0}, dy [] = {0,0,1,-1} ;

ll fast_power(int x ,int y){
    if (y==0)
        return 1;
    ll u = fast_power(x,y/2);
    u = (u*u)%mod ;
    if (y&1)
        u*=x ;
    return u%mod ;
}

int n , k ;
int dp[2005][2005] ;
vector<string>v ;
vector<string > numbers ={  "1110111", "0010010", "1011101", "1011011", "0111010", "1101011", "1101111", "1010010", "1111111", "1111011" } ;



int isvalid(string s , int x) {
    string t = numbers[x] ;
    int c  = 0;
    for(int i = 0 ;i<t.size() ;i++){
        if (s[i]=='1' && t[i] =='0')
            return -1 ;
        else if (s[i] != t[i])
            c++;
    }
    return c ;
}
bool m = 0;
string ans ;

int fn(int i ,int rem){
    if (i ==n && rem ==0)
        return -2;
    if (i == n || rem<0  )
        return -3 ;

    int& ret = dp[i][rem] ;
    if (~ret){
        return ret ;
    }
    int t;
    ret = -3 ;
    for(int j = 9 ;j>=0 ;j-- ){
        int x = isvalid(v[i] , j) ;
        if (x >=0 && x <=rem){
            t = fn(i+1 , rem-x);
            if (t != -3){
                ret = j ;
                ans+= to_string (j) ;
                m = 1; 
                return ret ;
            }
        }
    }
    return ret ;
}



void build(int i ,int rem){
    if (i == n)
        return ;
    int nxt = fn(i ,rem ) ;
    int cost = isvalid(v[i] , nxt) ;
    ans+=to_string(nxt) ;
    build(i+1 , rem - cost) ;
}

void solve(){
    cin>>n>>k;
    memset(dp ,-1 ,sizeof dp) ;
    fp(n) {
        string s ;cin>>s ;
        v.push_back(s);
    }
    fn(0 , k) ;
    if (!m)
        cout<<-1;
    else{
        reverse(ans.begin() ,ans.end());
        cout<<ans ;
    }
}


signed main() {
    momen
    int t=1 ;
    //seive();
    //pree() ;
    //cin >> t ;
    //string s ;
    for(int i=1 ;i<=t ;i++){
        solve() ;
    }
}


Comments

Submit
0 Comments
More Questions

680B - Bear and Finding Criminals
1036E - Covered Points
1015D - Walking Between Houses
155B - Combination
1531A - Зингер | color
1678A - Tokitsukaze and All Zero Sequence
896A - Nephren gives a riddle
761A - Dasha and Stairs
1728B - Best Permutation
1728A - Colored Balls Revisited
276B - Little Girl and Game
1181A - Chunga-Changa
1728C - Digital Logarithm
1728D - Letter Picking
792B - Counting-out Rhyme
1195A - Drinks Choosing
5D - Follow Traffic Rules
1272A - Three Friends
1632D - New Year Concert
1400D - Zigzags
716C - Plus and Square Root
412A - Poster
844B - Rectangles
1591A - Life of a Flower
1398C - Good Subarrays
629A - Far Relative’s Birthday Cake
1166A - Silent Classroom
1000B - Light It Up
218B - Airport
1463B - Find The Array